package com.xxgc.demo.controller.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Author:lxy
 * @create 2023/9/7 19:44
 * 全局统一返回对象
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)//如果对象转json时字段为空就直移除
public class Result<T> {
    @Schema(description = "系统内部状态码")
    private int code;

    @Schema(description = "系统提示信息")
    private String msg;

    @Schema(description = "总条数")
    private Long total;

    @Schema(description = "返回数据")
    private T data;

    @Schema(description = "异常信息")
    private String errorMsg;


    /**--------------部分内容改变---------------**/

    public  Result code(int code){
        this.code = code;
        return this;
    }

    public  Result  msg(String  msg){
        this.msg = msg;
        return this;
    }

    public  Result  errorMsg(String  errorMsg){
        this.errorMsg = errorMsg;
        return this;
    }

    public  Result data(T  data){
        this.data = data;
        return this;
    }

    public  Result  (int code, String msg, T data){
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public  Result total(Long total){
        this.total = total;
        return this;
    }

    public static <T> Result<T> ok(T data){
        return  new Result(200,"请求成功",data);
    }

    //成功的无参数版本
    public static <T> Result<T> ok(){
        return  new Result(200,"请求成功",null);
    }

    //注册成功
    public static <T> Result<T> success(){
        return  new Result(200,"注册成功",null);
    }

    public static <T> Result<T> error(int code,T data) {
        return new Result(code, "请求异常", data);
    }

    public static <T> Result<T> error() {
        return new Result(500, "请求异常", null);
    }

    public static <T> Result<T> error(String msg) {
        return new Result(500, "请求异常", msg);
    }
}
